import 'dart:io';
import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_native_image/flutter_native_image.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  File? file;
  String? tempDir;
  String? inputFilePath;
  String properties = '';
  String? filePath;

  Future<void> _loadLocalPath() async {
    tempDir = (await getTemporaryDirectory()).path;
    inputFilePath = '$tempDir/input.jpg';
    final ByteData bytes = await rootBundle.load('assets/test.jpg');
    File _file = File(inputFilePath ?? '');
    _file = await _file.writeAsBytes(bytes.buffer.asUint8List());

    setState(() {
      filePath = inputFilePath;
      file = _file;
    });

    // Response response = await Dio().download('http://loremflickr.com/320/240/music?lock=5', inputFilePath);
    // if(response.statusCode == 200) {
    //   file = File(inputFilePath??'');
    //   setState(() {});
    // }
  }

  Future<void> _reloadOtherTypePhoto(String type) async {
    tempDir = (await getTemporaryDirectory()).path;
    inputFilePath = '$tempDir/input-$type.$type';
    final ByteData bytes = await rootBundle.load('assets/test.$type');
    File _file = File(inputFilePath ?? '');
    _file = await _file.writeAsBytes(bytes.buffer.asUint8List());

    setState(() {
      filePath = inputFilePath;
      file = _file;
    });

  }

  @override
  initState() {
    super.initState();
    _loadLocalPath();
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('Flutter Native Image example app'),
        ),
        body: SingleChildScrollView(
            child: new Center(
          child: Column(
            children: [
              if (file != null) Image.file(file!),
              TextButton(
                onPressed: () {
                  file = File(inputFilePath ?? '');
                  filePath = inputFilePath;
                  setState(() {});
                },
                child: const Text('Reload'),
              ),
              TextButton(
                onPressed: () async {
                  await _reloadOtherTypePhoto('jpg');
                  setState(() {});
                },
                child: const Text('jpg'),
              ),
              TextButton(
                onPressed: () async {
                  await _reloadOtherTypePhoto('jpeg');
                  setState(() {});
                },
                child: const Text('jpeg'),
              ),
              TextButton(
                onPressed: () async {
                  await _reloadOtherTypePhoto('png');
                  setState(() {});
                },
                child: const Text('png'),
              ),
              TextButton(
                onPressed: () async {
                  await _reloadOtherTypePhoto('webp');
                  setState(() {});
                },
                child: const Text('webp'),
              ),
              TextButton(
                onPressed: () async {
                  if (file != null) {
                    file = await FlutterNativeImage.compressImage(
                        inputFilePath!,
                        targetWidth: Random().nextInt(150),
                        targetHeight: Random().nextInt(210));
                    filePath = file!.path;
                    setState(() {});
                  } else {
                    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                      content: Text('Null image data 😮'),
                    ));
                  }
                },
                child: const Text('compressImage'),
              ),
              Text(properties),
              SizedBox(height: 10),
              TextButton(
                onPressed: () async {
                  if (file != null) {
                    ImageProperties imageProperties =
                        await FlutterNativeImage.getImageProperties(
                            filePath!);
                    properties =
                        'width: ${imageProperties.width}\n height: ${imageProperties.height}\n info: ${imageProperties.orientation.toString()}';
                    setState(() {});
                  } else {
                    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                      content: Text('Null image data 😮'),
                    ));
                  }
                },
                child: const Text('getImageProperties'),
              ),
              TextButton(
                onPressed: () async {
                  if (file != null) {
                    file = await FlutterNativeImage.cropImage(
                        inputFilePath!,
                        Random().nextInt(10),
                        Random().nextInt(10),
                        Random().nextInt(150),
                        Random().nextInt(210));
                    filePath = file!.path;
                    setState(() {});
                  } else {
                    ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
                      content: Text('Null image data 😮'),
                    ));
                  }
                },
                child: const Text('cropImage'),
              ),
            ],
          ),
        )),
      ),
    );
  }
}
